package com.beiing.tianshuai.tianshuai.activity;

import android.annotation.SuppressLint;
import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.text.TextUtils;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.ImageView;
import android.widget.TextView;

import com.beiing.tianshuai.tianshuai.R;
import com.beiing.tianshuai.tianshuai.base.BaseActivity;
import com.beiing.tianshuai.tianshuai.widget.CustomDialog;

import jp.wasabeef.richeditor.RichEditor;

public class PublishEditTextActivity extends BaseActivity {

    /**
     * 富文本编辑器
     */
    private RichEditor mRichEditor;

    /**
     * 菜单按钮
     */
    private ImageView mBtnBold, mBtnH4, mBtnAlignLeft, mBtnAlignCenter, mBtnAlignRight;

    /**
     * toolbar标题
     */
    private TextView mToolbarTitle;

    /**
     * toolbar保存
     */
    private TextView mToolbarSave;

    /**
     * toolbar返回按钮
     */
    private ImageView mToolbarBack;

    /**
     * 是否为重新编辑文字
     */
    private boolean mIsEdit;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_publish_edit_text;
    }

    @Override
    protected void init() {
        Intent intent = getIntent();
        mIsEdit = intent.getBooleanExtra("isEdit", false);
        initView();
        initToolbar();
        initRichEditor();
        initListener();
    }

    /**
     * 初始化视图
     */
    private void initView() {
        mRichEditor = findViewById(R.id.re_content_container);
        mBtnBold = findViewById(R.id.iv_action_txt_bold);
        mBtnH4 = findViewById(R.id.iv_action_txt_h4);
        mBtnAlignLeft = findViewById(R.id.iv_action_align_left);
        mBtnAlignCenter = findViewById(R.id.iv_action_align_center);
        mBtnAlignRight = findViewById(R.id.iv_action_align_right);
//        mBtnInsertLink = findViewById(R.id.iv_action_insert_link);
        mToolbarBack = findViewById(R.id.toolbar_iv_back);
        mToolbarTitle = findViewById(R.id.toolbar_tv_title);
        mToolbarSave = findViewById(R.id.toolbar_btn_publish);
    }

    /**
     * 初始化
     */
    private void initToolbar() {
        mToolbarBack.setVisibility(View.VISIBLE);
        mToolbarTitle.setText("文本编辑");
        mToolbarSave.setVisibility(View.VISIBLE);
        mToolbarSave.setText("保存");
    }

    /**
     * 初始化富文本编辑器
     */
    @SuppressLint({"SetJavaScriptEnabled", "AddJavascriptInterface"})
    private void initRichEditor() {
        mRichEditor.setPadding(15, 10, 15, 10);
        if (mIsEdit) {
            String content = getIntent().getStringExtra("content");
            mRichEditor.setHtml(content);
        }
    }

    /**
     * 初始化监听事件
     */
    private void initListener() {
        // 返回
        mToolbarBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String content = mRichEditor.getHtml();
                if (!TextUtils.isEmpty(content)) {
                    CustomDialog dialog = new CustomDialog(mContext, R.style.dialog, "退出此次编辑？", new CustomDialog.OnCloseListener() {
                        @Override
                        public void onClick(Dialog dialog, boolean confirm) {
                            if (confirm) {
                                dialog.dismiss();
                                finish();
                            } else {
                                dialog.dismiss();
                            }
                        }
                    }).setTitle("提示");
                    dialog.setCanceledOnTouchOutside(true);
                    dialog.show();
                } else {
                    finish();
                }
            }
        });

        // 保存
        mToolbarSave.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent();
                String content = mRichEditor.getHtml();
                intent.putExtra("content", content == null ? "" : content);
                intent.putExtra("position", getIntent().getIntExtra("position", 0));
                setResult(TextUtils.isEmpty(content) ? NewDiscoveryPublishActivity.RESULT_CODE_WITHOUT_TXT : NewDiscoveryPublishActivity.RESULT_CODE_WITH_TXT, intent);
                InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
                if (imm != null) {
                    imm.hideSoftInputFromWindow(getWindow().getDecorView().getWindowToken(), 0);
                }
                finish();
            }
        });

        // 加粗
        mBtnBold.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mRichEditor.setBold();
            }
        });

        // H4标题
        mBtnH4.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mRichEditor.setHeading(4);
            }
        });

        // 居左
        mBtnAlignLeft.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mRichEditor.setAlignLeft();
            }
        });

        // 居中
        mBtnAlignCenter.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mRichEditor.setAlignCenter();
            }
        });

        // 居右
        mBtnAlignRight.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mRichEditor.setAlignRight();
            }
        });

//        // 插入链接
//        mBtnInsertLink.setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View v) {
//                mRichEditor.insertLink("http://www.baidu.com", "百度");
//            }
//        });
    }

    @Override
    public void onBackPressed() {
        String content = mRichEditor.getHtml();
        if (!TextUtils.isEmpty(content)) {
            CustomDialog dialog = new CustomDialog(mContext, R.style.dialog, "退出此次编辑？", new CustomDialog.OnCloseListener() {
                @Override
                public void onClick(Dialog dialog, boolean confirm) {
                    if (confirm) {
                        dialog.dismiss();
                        finish();
                    } else {
                        dialog.dismiss();
                    }
                }
            }).setTitle("提示");
            dialog.setCanceledOnTouchOutside(true);
            dialog.show();
        } else {
            finish();
        }
    }
}
